<?php


namespace App\Admin\Controllers\Imports;

use App\Imports\StaffImport;
use App\Models\Attendance;
use Carbon\Carbon;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;

/**
 * Created By FreeAbrams
 * 考勤汇总表
 * Date: 2020/11/27
 */
class AttendanceController
{
	// 文件后缀
	protected $extensions = [
		'xls',
		'xlsx'
	];
	public function index(Content $content, Request $request)
	{
		$import_time = new Carbon($request->post('dateTime'));
		//dd($import_time);
		$import_time = $import_time->startOfMonth();
		// 查询到导入记录后渲染查询到的数据
		return $content
					->header('考勤汇总表')
					->row(view('admin.import.date-input', ['route' => route('attendance.import')]))
					->row(view('admin.import.upload', ['route' => route('attendance.import')]))
					->row($this->grid($import_time));
	}
	
	public function import(Request $request)
	{
		// 验证文件后缀
		$file = $request->file('file');
		$canUpload = in_array($file->extension(), $this->extensions);
		if(!$canUpload){
			return Response()->json([
				'code' => 0,
				'msg' => '不是合法的文件'
			]);
		}

		// 验证导入时间选择
		$validate = Validator::make($request->all(), [
			'import_time' => 'required'
		], ['import_time.required' => '排班统计时间未选择']);
		if($validate->fails()){
			return Response()->json([
				'code' => 0,
				'msg' => $validate->errors()->first()
			]);
		}
		
		// 导入会覆盖之前的文件 原来的数据从磁盘上擦除然后重新导入
		$import_time = new Carbon($request->post('import_time'));
		$old = Attendance::where('import_time', $import_time)->delete();
		$res = 0;
		$res = Excel::import(new StaffImport($import_time), $file);
		
		if($res){
			return Response()->json([
				'code' => 1,
				'msg' => '导入成功'
			]);
		} else {
			return Response()->json([
				'code' => 0,
				'msg' => '导入失败'
			]);
		}
	}
	
	protected function grid($import_time)
    {
        $grid = new Grid(new Attendance());
        $grid->model()->where('import_time', $import_time);
        
        $grid->column('staff_id', '员工编号')->editable();
        $grid->column('name', __('Name'))->editable();
        $grid->column('department', __('Department'));
        $grid->column('base_working_time', __('Base working time'));
        $grid->column('actual_working_time', __('Actual working time'));
        $grid->column('skip', __('Skip'));
        $grid->column('skip_grade', __('Skip grade'));
        $grid->column('live_earl', __('Live earl'));
        $grid->column('live_earl_grade', __('Live earl grade'));
        $grid->column('base_exploited_times', __('Base exploited times'));
        $grid->column('special_exploited_times', __('Special exploited times'));
        $grid->column('base_actual_days', __('Base actual days'));
        $grid->column('travel_days', __('Travel days'));
        $grid->column('skip_work_days', __('Skip work days'));
        $grid->column('leave_days', __('Leave days'));
        $grid->column('work_rate', __('Work rate'));
        $grid->column('salary_remark', __('Salary remark'));
        $grid->column('exploited_salary', __('Exploited salary'));
        $grid->column('add_salary', __('Add salary'));
        $grid->column('late_reduce', __('Late reduce'));
        $grid->column('leave_reduce', __('Leave reduce'));
        $grid->column('reduce_salary', __('Reduce salary'));
        $grid->column('actual_salary', __('Actual salary'));
        $grid->column('remark', __('Remark'));
		
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('name', __('Name'));
        });
        $grid->disableExport();
        	$grid->tools(function (Grid\Tools $tools) {
			$tools->disableFilterButton();
			$tools->disableBatchActions();
		});
		$grid->disableColumnSelector();
		$grid->disableCreateButton();
		$grid->disableActions();
        return $grid;
    }
}